package com.example.zqh.config.other;

import com.example.zqh.model.RespBean;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class MyauthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest Request, HttpServletResponse Response, AuthenticationException e) throws IOException, ServletException {
       Response.setCharacterEncoding("UTF-8");
       Response.setContentType("application/json");
       PrintWriter out = Response.getWriter();
       RespBean bean = RespBean.error("尚未登录，请先登录");
       bean.setCode(401);
       out.write(new ObjectMapper().writeValueAsString(bean));
       out.flush();
       out.close();


    }
}
